package de.tabuma.formcreator.javascript.logik;

import de.tabuma.formcreator.entity.*;

import java.util.logging.*;
import java.util.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
 */
public class ControllerFacade {

    private static final Logger log = Logger.getLogger(ControllerFacade.class.getName());

    public static String getAllController(String frame, FormStructur formStructur) {
        String str = "";

        str += getSaveElementsController(frame, formStructur);
        str += getSaveController(formStructur);
        str += getSaveCancelController(formStructur);

        str += getRemoveController(formStructur);
        str += getRemoveCancelController(formStructur);
        str += getRemoveController1(formStructur);
        
        str += getAddElementController(formStructur);
        str += getEditElementController(formStructur);

        str += getUnshowDescriptionController(formStructur);

        for(int i=0;i<formStructur.getFormStructur().size();i++) {
            FormStructur fs = formStructur.getFormStructur().get(i);
            if(fs.isSimpleType()) {
                str += getShowDescription(fs);
            }
            else {
                if(fs.hasMinAndMaxOccurs1()) {
                    for(int j=0;j<fs.getFormStructur().size();j++) {
                        FormStructur fs1 = fs.getHeadStructur();
                        if(fs1.isSimpleType()) {
                            str += getShowDescription(fs1);
                        }
                    }
                }
            }
        }
        return str;
    }

    public static String getStartController(List<FormStructur> list) {
        String str = "";
        return str;
    }

    public static String getSaveElementsController(String frame, FormStructur fs) {
        String str = "";

        str += "\tfunction "+JSLib.getSaveElementsControllerName(fs)+"() {\n";
        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(fs1.isSimpleType()) {
                str += "\t\t"+JSLib.getObjectName(fs)+"."+JSLib.getObjectSetterName(fs1)+"("+frame+".document.getElementsByName(\""+JSLib.getElementName(fs1)+"\")[0].value);\n";
            }
            else {
                if(fs1.hasMinAndMaxOccurs1()) {
                    for(int j=0;j<fs1.getFormStructur().size();j++) {
                        FormStructur fs2 = fs1.getFormStructur().get(j);
                        if(fs2.isSimpleType()) {
                           str += "\t\t"+JSLib.getObjectName(fs1)+"."+JSLib.getObjectSetterName(fs2)+"("+frame+".document.getElementsByName(\""+JSLib.getElementName(fs2)+"\")[0].value);\n";
                        }
                    }
                    //TODO hinzufügen eines Elements
                    str += "\t\t" + JSLib.getObjectName(fs)+"."+JSLib.getObjectInsertElementIntoList(fs1)+"("+JSLib.getObjectName(fs1)+",0);\n";
                }
            }
        }
        str += "\t}\n";

        return str;
    }

    public static String getSaveController(FormStructur fs) {

        String str = "";

        str += "\tfunction "+JSLib.getSaveControllerName(fs)+"() {\n";
        str += "\t\t"+JSLib.getSaveElementsControllerName(fs)+"();\n";
        str += "\t\tif("+JSLib.getObjectCounter(fs)+"==-1) {\n";
        str += "\t\t\t"+JSLib.getObjectName(fs.getHeadStructur())+"."+JSLib.getObjectAddList(fs)+"("+JSLib.getObjectName(fs)+");\n";
        str += "\t\t}\n";
        str += "\t\telse {\n";
        str += "\t\t\t"+JSLib.getObjectName(fs.getHeadStructur())+"."+JSLib.getObjectInsertElementIntoList(fs)+"("+JSLib.getObjectName(fs)+","+JSLib.getObjectCounter(fs)+");\n";
        str += "\t\t}\n";
        str += "\t\t" + JSLib.getPrintView(fs.getHeadStructur())+"();\n";
        str += "\t}\n";

        return str;
    }
    
    public static String getSaveCancelController(FormStructur formStructur) {
        String str = "";

        str += "\tfunction "+JSLib.getSaveCancelControllerName(formStructur)+"() {\n";
        str += "\t\t"+JSLib.getPrintView(formStructur.getHeadStructur())+"();\n";
        str += "\t}\n";
        return str;
    }

    public static String getStartRemoveView(FormStructur formStructur) {
        String str = "";

        str += "\tfunction "+JSLib.getStartRemoveControllerName(formStructur)+"(i) {\n";
        str += "\t\t"+JSLib.getObjectCounter(formStructur)+"=i;\n";
        str += "\t\t"+JSLib.getRemoveView(formStructur)+"();\n";
        str += "\t}\n";

        return str;
    }

    public static String getRemoveController(FormStructur fs) {
        
        String str = "";

        str += "\tfunction "+JSLib.getRemoveController1Name(fs)+"() {\n";
        str += "\t\t"+JSLib.getObjectName(fs.getHeadStructur())+"."+JSLib.getObjectRemoveList(fs)+"("+JSLib.getObjectCounter(fs)+");\n";
        str += "\t}\n";

        return str;
    }

    public static String getRemoveCancelController(FormStructur formStructur) {
        String str = "";
        str += "\tfunction "+JSLib.getRemoveCancelControllerName(formStructur)+"() {\n";
        str += "\t\t"+JSLib.getPrintView(formStructur)+"();\n";
        str += "\t}\n";
        return str;
    }

    public static String getRemoveController1(FormStructur fs) {
        String str = "";

        str += "\tfunction "+JSLib.getRemoveController1Name(fs)+"() {\n";
        str += "\t\t"+JSLib.getPrintView(fs.getHeadStructur())+"();\n";
        str += "\t}\n";

        return str;
    }

    public static String getAddElementController(FormStructur fs) {
        String str = "";

        str += "\tfunction "+JSLib.getAddElementControllerName(fs)+"() {\n";
        str += "\t\t"+JSLib.getSaveElementsControllerName(fs.getHeadStructur())+"();\n";
        str += "\t\t"+JSLib.getObjectName(fs)+" = new "+JSLib.getObjectContructurName(fs)+"();\n";
        str += "\t\t"+JSLib.getObjectCounter(fs)+" = -1;\n";
        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(!fs1.isSimpleType() && fs1.hasMinAndMaxOccurs1()) {
                str += "\t\t"+JSLib.getObjectName(fs1)+" = new "+JSLib.getObjectContructurName(fs1)+"();\n";
                str += "\t\t"+JSLib.getObjectCounter(fs1)+" = -1;\n";
            }
        }
        str += "\t\t"+JSLib.getPrintView(fs)+"();\n";
        str += "\t}\n";

        return str;
    }

    public static String getEditElementController(FormStructur fs) {
        String str = "";

        str += "\tfunction "+JSLib.getEditElementControllerName(fs)+"(i) {\n";
        str += "\t\t"+JSLib.getObjectName(fs)+" = "+JSLib.getObjectName(fs.getHeadStructur())+"."+JSLib.getObjectGetterName(fs)+"()[i];\n";
        str += "\t\t"+JSLib.getObjectCounter(fs)+" = i;\n";
        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(!fs1.isSimpleType() && fs1.hasMinAndMaxOccurs1()) {
                str += "\t\t"+JSLib.getObjectName(fs1)+" = "+JSLib.getObjectName(fs1.getHeadStructur())+"."+JSLib.getObjectGetterName(fs1)+"()[0];\n";
                str += "\t\t"+JSLib.getObjectCounter(fs1)+" = 0;\n";
            }
        }
        str += "\t\t"+JSLib.getPrintView(fs)+"();\n";
        str += "\t}\n";

        return str;
    }

    public static String getSendDataController(FormStructur fs) {
        String str = "";
        str += "\tfunction "+JSLib.getSendDataControllerName()+"() {\n";
        str += "\t\t"+JSLib.getSaveElementsControllerName(fs)+"();\n";
        str += "\t\txml = "+JSLib.getObjectName(fs)+"."+JSLib.getStructurAsXML()+"();\n";
        str += "\t\t"+JSLib.getLoggingFunction()+"('trying to send "+JSLib.getObjectName(fs)+"."+JSLib.getStructurAsXML()+"'+xml);\n";
        str += "\t\t"+JSLib.getSendDataFunction()+"(xml);\n";
        str += "\t}\n";
        return str;
    }

    public static String getShowDescription(FormStructur fs) {
        String str = "";

        str += "\tfunction "+JSLib.getShowDescriptionControllerName(fs)+"() {\n";
        if(fs.getHeadStructur().hasMinAndMaxOccurs1()) {
            str += "\t\t"+JSLib.getSaveElementsControllerName(fs.getHeadStructur().getHeadStructur())+"();\n";
            str += "\t\t"+JSLib.getDescriptionVar(fs.getHeadStructur().getHeadStructur())+"='"+fs.getDescriptionWithoutSpecialCharacter()+"';\n";
            str += "\t\t"+JSLib.getPrintView(fs.getHeadStructur().getHeadStructur())+"();\n";
        }
        else {
            str += "\t\t"+JSLib.getSaveElementsControllerName(fs.getHeadStructur())+"();\n";
            str += "\t\t"+JSLib.getDescriptionVar(fs.getHeadStructur())+"='"+fs.getDescriptionWithoutSpecialCharacter()+"';\n";
            str += "\t\t"+JSLib.getPrintView(fs.getHeadStructur())+"();\n";
        }
        str += "\t}\n";

        return str;
    }

    public static String getUnshowDescriptionController(FormStructur fs) {
        String str = "";
        str += "\tfunction "+JSLib.getUnshowDescriptionControllerName(fs)+"() {\n";
        str += "\t\t"+JSLib.getSaveElementsControllerName(fs)+"();\n";
        str += "\t\t"+JSLib.getDescriptionVar(fs)+"='';\n";
        str += "\t\t"+JSLib.getPrintView(fs)+"();\n";
        str += "\t}\n";
        return str;
    }
}